<?php
/**
 * Created by PhpStorm.
 * User: andy
 * Date: 2019/8/29
 * Time: 下午6:42
 */
namespace app\common\model;

use think\Model;
use think\facade\Cache;

class AdPositionModel extends Model
{
    const STATUS_SHOW=1; //启用
    const STATUS_HIDE=0; //禁用

    protected $pk='position_id'; //指定主键

    /**
     * 所有分组
     * @param int $pageSum 每页显示条数
     * @param bool $status 是否需要分页
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getALL($pageSum=0)
    {
        if($pageSum<=0){
            $pageSum=getSystem("pageSUM")?getSystem("pageSUM"):10;
        }

        $list=self::where('position_id','>',0)->paginate($pageSum);

        return $list;
    }



    /**
     * 增加分组
     * @param $data
     * @return bool
     */
    public function adds($data){
        //检测当前应用方法名是否重复
        if(!self::chackAction($data)){
            return['status'=>0,'msg'=>'名称已存在！'];
        }

        if(empty($data['position_id'])){
            $status = self::save($data);

            if($status){
                return['status'=>1,'msg'=>'增加成功！'];
            }else{
                return['status'=>0,'msg'=>'增加失败！'];
            }
        }else{
            $status = self::save($data,['position_id'=>$data["position_id"]]);

            if($status){
                return['status'=>1,'msg'=>'修改成功！'];
            }else{
                return['status'=>0,'msg'=>'修改失败！'];
            }
        }

    }

    /**
     * 删除
     * @param $id
     * @return array
     */
    public function del($id){

        $status=self::destroy($id);
        if($status){
            return ['status'=>1,'msg'=>'删除成功！'];
        }else{
            return ['status'=>0,'msg'=>'删除失败！'];
        }

    }

    /**
     * 检测当前是否重复
     * @param $data
     * @return bool
     */
    private static function chackAction($data){
        if(empty($data['position_id'])){
            $data['position_id']=0;
        }

        $count=self::where('position_name',$data['position_name'])
            ->where('position_id',"<>",$data['position_id'])
            ->count();

        if($count>0){
            return false;
        }else{
            return true;
        }
    }

}